﻿using System;
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json.Linq;
using SkiTracker.Model;

namespace SkiTracker.Converter
{
    public class EnumJsonConverter<TEnum>  : IDataMemberJsonConverter where TEnum : struct 
    {
        public object ConvertFromJson(JToken value)
        {
            if (value != null)
            {
                TEnum result;
                if (Enum.TryParse(value.ToString(), out result))
                    return result;
            }

            return PlatformType.Unknown;
        }

        public JToken ConvertToJson(object instance)
        {
            if (instance is TEnum)
                return instance.ToString();
            else
                return "null";
        }
    }
}
